﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using Ebada.UI.Base;
using Ebada.Client;
using Ebada.Core;
using Ebada.Client.Platform;
namespace Ebada.Modules.Vehicle
{
    /// <summary>
    /// 违章记录编辑
    /// </summary>
    public partial class frmVehicleActivityNEdit : FormBase, IPopupFormEdit {

        private string operType = "";
        int day = 0;

        public string OperType
        {
            get { return operType; }
            set
            {
                operType = value;
                if (operType == "2")
                {
                    this.Text = "编辑年审费用登记";
                    groupControlOrg.Text = "年审费用登记";
                }
                if (operType == "3")
                {
                    this.Text = "编辑养路费用登记";
                    groupControlOrg.Text = "养路费用登记";
                  
                }
                if (operType == "4")
                {
                    this.Text = "编辑二维费用登记";
                    groupControlOrg.Text = "二维费用登记";
                 
                }
            }
        }
        public frmVehicleActivityNEdit() {
            InitializeComponent();
        }
        void dataBind() {

            this.txtVehicleNumber.DataBindings.Add("EditValue", rowData, "VehicleNumber");
            this.txtAct_Code.DataBindings.Add("EditValue", rowData, "Act_Code");
            this.txtAct_Name.DataBindings.Add("EditValue", rowData, "Act_Name");
            this.txtAccountCode.DataBindings.Add("EditValue", rowData, "AccountCode");
            this.txtAccountTitle.DataBindings.Add("EditValue", rowData, "AccountTitle");

            this.dateDate3.DataBindings.Add("EditValue", rowData, "Date3");
            this.txtBOrgcnName.DataBindings.Add("EditValue", rowData, "BOrgcnName");
            this.txtTotalPrice.DataBindings.Add("EditValue", rowData, "TotalPrice");
            this.dateStartDate.DataBindings.Add("EditValue", rowData, "StartDate");
            this.dateExpirationDate.DataBindings.Add("EditValue", rowData, "ExpirationDate");
            this.txtEmployeeName.DataBindings.Add("EditValue", rowData, "EmployeeName");
            this.txtRemark.DataBindings.Add("EditValue", rowData, "Remark");
        }
        #region IPopupFormEdit Members
        private VehicleActivity rowData = null;

        public object RowData {
            get {
                return rowData;
            }
            set {
                if (value == null) return;
                if (rowData == null) {
                    this.rowData = value as VehicleActivity;
                    dataBind();
                    InitComboBoxData();
                } else {
                    ConvertHelper.CopyTo<VehicleActivity>(value as VehicleActivity, rowData);
                    InitComboBoxData();
                }
            } 
        }

        #endregion

        private void InitComboBoxData() {
           
            IList<Vehicles> vlist = MainHelper.GetSqlMap<Vehicles>().GetListByWhere<Vehicles>(" order by VehicleNumber");
            txtVehicleNumber.Properties.DataSource = vlist;
            IList<Accounts> acclist = MainHelper.GetSqlMap<Accounts>().GetListByWhere<Accounts>(" where DebitOrCredit like '%行政%' order by AccountCode");
            txtAccountTitle.Properties.DataSource = acclist;

            if (operType == "2")
            {
                IList<Activities> list = MainHelper.GetSqlMap<Activities>().GetListByWhere<Activities>(" where ProjectName='车辆项目' and Act_Name='年审'");
                if (list != null && list.Count > 0)
                {
                    rowData.Act_Code = list[0].Act_Code;
                    rowData.Act_Name = list[0].Act_Name;
                }
            }
            if (operType == "3")
            {
                IList<Activities> list = MainHelper.GetSqlMap<Activities>().GetListByWhere<Activities>(" where ProjectName='车辆项目' and Act_Name='养路'");
                if (list != null && list.Count > 0)
                {
                    rowData.Act_Code = list[0].Act_Code;
                    rowData.Act_Name = list[0].Act_Name;
                }
            }
            if (operType == "4")
            {
                IList<Activities> list = MainHelper.GetSqlMap<Activities>().GetListByWhere<Activities>(" where ProjectName='车辆项目' and Act_Name='二维'");
                if (list != null && list.Count > 0)
                {
                    rowData.Act_Code = list[0].Act_Code;
                    rowData.Act_Name = list[0].Act_Name;
                }
            }
            if (rowData.VehicleNumber=="")
            {
                rowData.Date3 = DateTime.Now;
                rowData.StartDate = DateTime.Now;
                rowData.ExpirationDate = DateTime.Now;
            }
            string str = "";
            if (operType == "2")
            {
                str = "年审";
            }
            if (operType == "3")
            {
                str = "养路";
            }
            if (operType == "4")
            {
                str = "二维";

            }
            IList<VehicleWarningType> v = MainHelper.GetSqlMap<VehicleWarningType>().GetListByWhere<VehicleWarningType>(" where Act_Name='" + str + "'");
            if (v.Count > 0)
            {
                day = v[0].Days;
            }
        }
        public void SetComboBoxData(DevExpress.XtraEditors.LookUpEdit comboBox, string displayMember, string valueMember, string nullTest, string cnStr, IList<mOrg> post)
        {
            comboBox.Properties.Columns.Clear();
            comboBox.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            comboBox.Properties.DataSource = post;
            comboBox.Properties.DisplayMember = displayMember;
            comboBox.Properties.ValueMember = valueMember;
            comboBox.Properties.NullText = nullTest;
            comboBox.Properties.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo(valueMember, "ID", 20, DevExpress.Utils.FormatType.None, "", false, DevExpress.Utils.HorzAlignment.Default),
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo(displayMember, cnStr)});
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            
            if (txtAccountTitle.Text == "")
            {
                MessageBox.Show("科目名称不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtAccountTitle.Focus();
                return;
            }
            if (txtVehicleNumber.Text == "")
            {
                MessageBox.Show("车牌号不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtVehicleNumber.Focus();
                return;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

       
        private void txtAccountTitle_EditValueChanged(object sender, EventArgs e)
        {
            if (txtAccountTitle.Text != "")
            {
                rowData.AccountCode = ((Accounts)txtAccountTitle.Properties.GetDataSourceRowByKeyValue(txtAccountTitle.Text)).AccountCode;
                txtAccountCode.Text = rowData.AccountCode;
            }
        }

        private void dateExpirationDate_EditValueChanged(object sender, EventArgs e)
        {
            rowData.Date3 = dateExpirationDate.DateTime.AddDays(-day);
            dateDate3.DateTime = rowData.Date3;
        }

        private void txtVehicleNumber_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmVehiclesSeletcter frm = new frmVehiclesSeletcter();
            frm.InitData_Slect("");
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtVehicleNumber.Text = frm.SelectObj.VehicleNumber;
                txtVehicleNumber.ClosePopup();
            }
        }

        private void txtEmployeeName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            NameClick();
        }

        private void txtEmployeeName_Click(object sender, EventArgs e)
        {
            NameClick();
        }

        public void NameClick()
        {
            Ebada.Client.Platform.Selector.frmEmpSelectorByOrgInfo frm = new Ebada.Client.Platform.Selector.frmEmpSelectorByOrgInfo();
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.InitionData(string.Format(" where 1=1", this.txtEmployeeName.Text), false, false);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtEmployeeName.Text = frm.CurrentEmp.Empolyee_Name;
                rowData.EmployeeID = frm.CurrentEmp.Empolyee_ID;
            }
        }

        private void txtBOrgcnName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            OrgClick();
        }
        public void OrgClick()
        {
            frmOrgSelector2 frm = new frmOrgSelector2();
            frm.IntionDataAll();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                rowData.BOrgcnName = frm.SelectObj.cnName;
                txtBOrgcnName.Text = frm.SelectObj.cnName;
                rowData.BOrgCode = frm.SelectObj.OrgCode;
            }
        }

        private void txtBOrgcnName_Click(object sender, EventArgs e)
        {
            OrgClick();
        }
    }
}